package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.CostAnalysis;
import com.dy.nygl.service.CostAnalysisService;

/**
 * 本类完成：
 * loadingCost.action  ---- > 显示cost_analysis表中的所有信息，
 * loadingCostOne.action  ---- > 通过id拿到单个能源对象，
 * addCost.action  ---- > 添加能源对象
 * updateCost.action  ---- > 修改能源对象
 * deleteCost.action  ---- > 删除能源对象
 * checkEneryType.action  ---- > 用于一般校验
 * 
 * 未完成：校验模块
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/costAnalysis")
public class CostAnalysisController {
	
	@Resource(name="costAnalysisService")
	private CostAnalysisService costAnalysisService;
	
	//加载就拿到现实存在的内容。
	@RequestMapping("/loadingCost.action")
	@ResponseBody
	public ModelAndView loadingCost(){
		System.out.println("进入loadingCost。。。。");
		ModelAndView model = new ModelAndView();
		List<CostAnalysis> costAnalysis = costAnalysisService.selectAll();
		model.addObject("costAnalysis",costAnalysis);
		model.setViewName("costAnalysisUpdate");
		System.err.println(costAnalysis);
		return model;
	}
	
	@RequestMapping("/loadingCostOne.action")
	public ModelAndView loadingCostOne(@RequestParam("costAnalysisId")Integer id){
		ModelAndView model = new ModelAndView();
		model.addObject("selectOne",costAnalysisService.selectOne(id));
		model.setViewName("costUpdate");
		return model;
	}
	//添加需对 能源类型 进行验证
	@RequestMapping(value="/addCost.action",method={RequestMethod.GET})
	public ModelAndView addCost(HttpServletRequest request, HttpServletResponse response) throws IOException{
		request.setCharacterEncoding("utf-8");
		CostAnalysis cost = new CostAnalysis();
		cost.setCode(String.valueOf(costAnalysisService.selectCode()+1));
		System.err.println(costAnalysisService.selectCode());
		cost.setEnergyType(request.getParameter("energyType"));
		cost.setUnitPrice(Double.valueOf(request.getParameter("unitPrice")));
		costAnalysisService.addCost(cost);
		return new ModelAndView("redirect:/costAnalysis/loadingCost.action");
	}
	
	@RequestMapping(value="/updateCost.action",method={RequestMethod.POST})
	public ModelAndView updateCost(@RequestParam("selectOneId") Integer id, HttpServletRequest request) throws IOException{
		CostAnalysis cost = new CostAnalysis();
		System.err.println(id);
		cost.setId(id);
		cost.setCode(request.getParameter("code"));
		cost.setEnergyType(request.getParameter("energyType"));
		cost.setUnitPrice(Double.valueOf(request.getParameter("unitPrice")));
		costAnalysisService.updateCost(cost);
		return new ModelAndView("redirect:/costAnalysis/loadingCost.action");
	}
	
	@RequestMapping("/deleteCost.action")
	public ModelAndView deleteCost(@RequestParam("costAnalysisId") Integer id){
		costAnalysisService.deleteCost(id);
		String time = new Date().toLocaleString();
		return new ModelAndView("redirect:/costAnalysis/selectA.action?time="+time);
	}
	
	@RequestMapping("/selectA.action")//带参跳转controller之间
	public ModelAndView selectA(@RequestParam("time")String time){
		System.out.println(time);
		return new ModelAndView("redirect:/costAnalysis/loadingCost.action");
	}
	
	@RequestMapping(value="/checkEneryType.action",method={RequestMethod.GET})
	public void checkEneryType(HttpServletRequest request,HttpServletResponse response) throws IOException{
		System.err.println("进入验证处理。。。。。");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		System.err.println(costAnalysisService.checkEneryType(request.getParameter("energyType")));
		out.println(costAnalysisService.checkEneryType(request.getParameter("energyType"))); 
	}

}
